package com.hs.utils;

import com.hs.entity.pojo.User;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.Function;

/**
 * @author 何森
 * @since 2023-08-07-12:49
 */
public class OptUtil<T> {

    @SuppressWarnings({"unchecked", "rawtypes"})
    private static final OptUtil EMPTY = new OptUtil(null);

    private final T obj;

    private OptUtil(T obj) {
        this.obj = obj;
    }

    public static <T, R> R of(T obj, Function<T, R> func) {
        if (Objects.isNull(obj)) return null;
        Objects.requireNonNull(func);
        return func.apply(obj);
    }

    public static <T> OptUtil<T> of(T obj) {
        return new OptUtil<>(obj);
    }

    @SuppressWarnings("unchecked")
    public <R> OptUtil<R> get(Function<T, R> func) {
        if (Objects.isNull(obj)) return (OptUtil<R>) EMPTY;
        Objects.requireNonNull(func);
        return of(func.apply(obj));
    }

    public T get() {
        return obj;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(of(new User()).get(User::getArea).get(String::getBytes).get()));
    }

}
